﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Volo.Abp.Identity;
using Rechargeable.Products;
using Rechargeable.Machines;
using Rechargeable.Machines.Dto;
using Rechargeable.Users;
using Microsoft.AspNetCore.Mvc.Rendering;
using Volo.Abp.Users;

namespace Rechargeable.Web.Pages.Products.Prices
{
    public class CreateSerialNumbersModel : RechargeablePageModel
    {
        private readonly IUserAppService userAppService;
        private readonly IProductAppService productAppService;
        private readonly IProductPriceAppService productPriceAppService;
        private readonly ISerialNumberAppService serialNumberAppService;

        [BindProperty(SupportsGet = true)]
        public Guid ProductId { get; set; }

        [BindProperty(SupportsGet = true)]
        public Guid ProductPriceId { get; set; }

        public string ProductName { get; set; }

        public string ProductPriceDesc { get; set; }

        public List<IdentityUserDto> Users { get; set; }

        public CreateSerialNumbersDto CreateSerialNumbersDto { get; set; }

        [BindProperty]
        public int Count { get; set; }

        [BindProperty]
        public string Desc { get; set; }

        [BindProperty]
        public Guid UserId { get; set; }

        public CreateSerialNumbersModel(IUserAppService _userAppService,
            ISerialNumberAppService _serialNumberAppService,
            IProductAppService _productAppService,
            IProductPriceAppService _productPriceAppService)
        {
            userAppService = _userAppService;
            productPriceAppService = _productPriceAppService;
            productAppService = _productAppService;
            serialNumberAppService = _serialNumberAppService;
        }

        public async Task OnGetAsync()
        {
            Users = await userAppService.GetAllUsersAsync();
            ProductName = (await productAppService.GetAsync(ProductId)).Name;
            ProductPriceDesc = (await productPriceAppService.GetAsync(ProductPriceId)).Desc;
        }

        public async Task<IActionResult> OnPostAsync()
        {
            await serialNumberAppService.PostRechargesAsync(new CreateSerialNumbersDto() { ProductId = ProductId, ProductPriceId = ProductPriceId, BuyUserId = UserId, Count = Count, Desc = Desc });
            return new NoContentResult();
        }

        public IEnumerable<SelectListItem> UserSelectListItems
        {
            get
            {
                var items = new List<SelectListItem>();
                foreach (var user in Users)
                {
                    items.Add(new SelectListItem() { Value = user.Id.ToString(), Text = user.UserName });
                }
                return items;
            }
        }
    }
}